package com.zg.common.core.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

import java.security.KeyPair;

/**
 * @author Zxb
 */
public class RsaUtil {

    private static final String PRI_KEY_2 = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAO3fQBw7wJIB/NJrkvyKRWt96EfpoXD0rIvzk3M4B245+c9xuIAYuMtYQo6Q6GRiuVIB9Tmz6lrc5BOyCl8KRVWbnp0RCHyxqMhHi7IZuG/T8J289NlLmjew/piL1N6ZZnjjf5A2NqM3oxIJOZNjzpmhH2OZNwm1XiQ1NBYOi/qpAgMBAAECgYAt/RgZfUnlUKIPEoI39gmm1xBV+urd+kfzxNNQltgE7QuJmjxn85+inL09b/GmGtomaz98ePbJu2QTFLxhlIxDueUNuZWp+daDfftV/gaCKJU0sZ/t22OtwegpT3NwcAFx8c74A0k3Sera34R+adUL/ALnQXff6gbMPjw0LU/LCQJBAPpzGlimfeuM2x/F/QwAceGkqMtKyR7kUqN6iux4Bdq8e2wrZwlLYmDsQwDnQ8vujlrFunFXB9hFPmF/nbf1RyUCQQDzJMpNOx7mGl0txsdSOMVsO6qdim78zh3qmBoJLaGWfWuQoF2KR2dyWITaxfuJTGoDGiSOioZYkn4Yy4Zrn0A1AkAS+kHW0dbc0HGSOMhx7l6ywG1h6irPQnHYnWKFhJq89mdLzQYjuCieO4jgOqXygAMdcbos+HFJvAV8EdV7qQRZAkEAl4ChHaZtBmv/C3sEz9LYESzMjwWnSpcURQ0pi/aG7UKRN7j1ECiV6MYHYlpi9ZZLqEst5DOkqqP9Nm37ACxm0QJACfN90x3r+wFR+MJ9BS5yXUNcfRRpcPBOD7D4QrzOMkWUh3QkuaQZSESpud7UUt0PAfFfTFLx4XQyeY9CqdrYTg==";
    private static final String PUB_KEY_2 = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDt30AcO8CSAfzSa5L8ikVrfehH6aFw9KyL85NzOAduOfnPcbiAGLjLWEKOkOhkYrlSAfU5s+pa3OQTsgpfCkVVm56dEQh8sajIR4uyGbhv0/CdvPTZS5o3sP6Yi9TemWZ443+QNjajN6MSCTmTY86ZoR9jmTcJtV4kNTQWDov6qQIDAQAB";
    private static final String PUB_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8YfiQd2Y0XgS9sdDS6nRJJLkSy2ryURe3QHBmvPmuM17CXlHI3KO7mDHgo2qnJBW5amXZxJxf1y0pSTBWS6n+A2A/MDnsGFW3RYGtEX1VmNXTqgQgErNAaZan4BFQLw+yBRB23ULXO1zeesL5izJkTr9xFA54KBG5JyC/xk37IQIDAQAB";
    private static final String PRI_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALxh+JB3ZjReBL2x0NLqdEkkuRLLavJRF7dAcGa8+a4zXsJeUcjco7uYMeCjaqckFblqZdnEnF/XLSlJMFZLqf4DYD8wOewYVbdFga0RfVWY1dOqBCASs0BplqfgEVAvD7IFEHbdQtc7XN56wvmLMmROv3EUDngoEbknIL/GTfshAgMBAAECgYAfMM16BfrrIsIAMofT1DPqxGTMQtfzRQ/7LHUv3Ywtlp3S6tD5w+ZsMhfYDNak0+mw6HfypYYKrFLy6gZR03dSqa6ronmZRHQKhIL2O1t5/Dvl759AqTxwbir7BDP/UNbHXHaulPxyjTQDZ7RkEouSEGjgHMWRf/dS2XBM4qTIkQJBAOSmL6ER1Xda8RMsB4Gz505g/OLN5xsy+lXl+erqRLGvxRMx+3a3olw5tUrO3Xv0WmA74wZopOCty05eFcpJuz0CQQDS6rlljtC5P7Af8eGGl7zEAbReStvuTDaiG24o492JKysg3Sjn+G0Zrhrrrqi97ZkpNoOEEKUSkkTKOVlk/I21AkEA3XyODryzx5Z0iFnSO7YFjkstLJbAE/zhumu4F/Mu7+CQSvPT30ArKqNMFlck/XnKQgZA+GhNrscrdZJ2ESozEQJBAJJQIFJyUxrfMnfGnNN4eYtjP1NWDOFvX0rvSmj1QZ5BIwcGRLU+4bg5zhUGsaono+MyHOPFhC2w6CV8ySXECS0CQDE4LIwa7ydiliZf+Od0EH18nMzzLrXUMu2WHI1/GrDmOtSsQIVRCZAhPTqWlyVYV0yzofvJqkbTHnhMD8uQGys=";

    public static void generateKey() {
        KeyPair pair = SecureUtil.generateKeyPair("RSA");
        String privateKey = Base64.encode(pair.getPrivate().getEncoded());
        System.out.println("私钥\t" + privateKey);
        String publicKey = Base64.encode(pair.getPublic().getEncoded());
        System.out.println("公钥\t" + publicKey);
    }

    public static String encrypt(String text) {
        RSA rsa = new RSA(PRI_KEY, PUB_KEY);
        // 私钥加密
        return rsa.encryptBase64(text, KeyType.PrivateKey);
    }

    public static String encryptByPub(String publicKey, String text) {
        RSA rsa = new RSA(null, publicKey);
        // 私钥加密
        return rsa.encryptBase64(text, KeyType.PublicKey);
    }

    public static String decrypt(String encryptByPublic) {
        RSA rsa = new RSA(PRI_KEY, PUB_KEY);
        // 公钥解密
        return rsa.decryptStr(encryptByPublic, KeyType.PublicKey);
    }

    public static String encrypt2(String text) {
        RSA rsa = new RSA(null, PUB_KEY_2);
        // 私钥加密
        return rsa.encryptBase64(text, KeyType.PublicKey);
    }

    public static String decrypt2(String encryptByPublic) {
        RSA rsa = new RSA(PRI_KEY_2, PUB_KEY_2);
        // 公钥解密
        return rsa.decryptStr(encryptByPublic, KeyType.PrivateKey);
    }

    public static String decryptByPri(String encryptByPublic) {
        RSA rsa = new RSA(PRI_KEY, PUB_KEY);
        // 公钥解密
        return rsa.decryptStr(encryptByPublic, KeyType.PrivateKey);
    }

    public static void main(String[] args) {
        String xxx = "ayj1wNGr5kPw+wJSDpQVyYEcihOx5yKXUkbL6sVpxy9oxcMfZ4EKsFiDE4AqbkMz34027C2E9IV47JlSs8nOmqAchml/kyG2dnYvitG33bJgApW6KRGShwxRMIAMJ4puVSB3rbJ+w0gp3O4cg3hp6HWWw0dFRr0IDxAIGyQiEn8=";
        String yyy = RsaUtil.decryptByPri(xxx);
        System.out.println(yyy);
        //测试生成
        RsaUtil.generateKey();
        //测试加解密
        String text = "Ab1230bq189";
        String encryptByPublic = RsaUtil.encrypt(text);
        System.out.println(encryptByPublic);
        String decryptByPrivate = RsaUtil.decrypt(encryptByPublic);
        System.out.println(decryptByPrivate);


        String str = "Rt9bCMs8a+HqvckUVef+LvpEdQSc3XZCdB32PgWnlCoukCw9Cel9JZQflbPZW2ZBGqtUjYcSHG98jyVfCZdjpb1FHKMTMVNLNsAhMjp+1ym83m2w4znHxNPPy4F+OrZeOxA2Yn+wN2Br5CCrFomO7m2ZFGH6kT4FvLv9mpPq6Ps=";

        String str2 = RsaUtil.decrypt(str);

        String key = "gxvwq4logxvwq4logxvwq4lo";
        String str3 = DesUtil.decrypt("4a82e7b4b695da113662591cf208499f66eb064505f54173b417c9e00f2ab36b3d8413e35a894961bbe451f440bc5b90", key);
        System.out.println(str2);
        System.out.println(str3);

    }
}
